package com.climber.android.commonres.helper

import android.view.Gravity
import android.widget.FrameLayout
import android.widget.TextView
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import com.climber.android.commonres.R
import io.ganguo.library.mvp.util.RxLifecycleUtils
import io.ganguo.library.mvp.util.UIUtils
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import java.util.concurrent.TimeUnit

/**
 * Created by Mikes at 2019-06-29 11:25
 */
fun TextView.countDownTick(millisUntilFinished: Long) {
    val newLayoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.MATCH_PARENT)
    newLayoutParams.gravity = Gravity.RIGHT or Gravity.CENTER_VERTICAL

    this.layoutParams = newLayoutParams
    this.isEnabled = false
    this.text = context.getString(R.string.resend_again_count_down,millisUntilFinished)
}

fun TextView.countDownFinish() {
    val newLayoutParams = FrameLayout.LayoutParams(UIUtils.dip2Px(80), FrameLayout.LayoutParams.MATCH_PARENT)
    newLayoutParams.gravity = Gravity.RIGHT or Gravity.CENTER_VERTICAL

    this.layoutParams = newLayoutParams
    this.isEnabled = true
    this.setText(R.string.resend_again)
}

object RxCountDown {
    fun startSMSCodeCountDown(lifecycleOwner: LifecycleOwner, sendSmsButton : TextView){
        val count = 60L

        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(count)
                .map { increaseTime ->
                    count - increaseTime
                }
                .observeOn(AndroidSchedulers.mainThread())
                .`as`(RxLifecycleUtils.bindUntilEvent(lifecycleOwner, Lifecycle.Event.ON_DESTROY))
                .subscribe(object : Observer<Long> {
                    override fun onComplete() {
                        sendSmsButton.countDownFinish()
                    }

                    override fun onSubscribe(d: Disposable) {
                    }

                    override fun onNext(millisUntilFinished: Long) {
                        sendSmsButton.countDownTick(millisUntilFinished)
                    }

                    override fun onError(e: Throwable) {
                    }
                })

    }

    fun startSMSCodeCountDown(lifecycleOwner: LifecycleOwner,
                              countDownTickCallback : ((millisUntilFinished : Long)->Unit),
                              countDownFinishCallback : (()->Unit)){
        val count = 60L

        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(count)
                .map { increaseTime ->
                    count - increaseTime
                }
                .observeOn(AndroidSchedulers.mainThread())
                .`as`(RxLifecycleUtils.bindUntilEvent(lifecycleOwner, Lifecycle.Event.ON_DESTROY))
                .subscribe(object : Observer<Long> {
                    override fun onComplete() {
                        countDownFinishCallback()
                    }

                    override fun onSubscribe(d: Disposable) {
                    }

                    override fun onNext(millisUntilFinished: Long) {
                        countDownTickCallback(millisUntilFinished)
                    }

                    override fun onError(e: Throwable) {
                    }
                })

    }
}